import requests
from lxml import etree

from Wallpaper.models import Wallpaper, Category


class NetBiAnWallpaperSpider:
    def __init__(self):
        self.classes = {
            # '全部': 2,
            '动漫': 24, '游戏': 25, '美女': 26, '风景': 33, '汽车': 34, '动物': 35, '新年': 36, '鬼刀': 37,
            '集原美': 38, '英雄联盟': 39, '王者荣耀': 41, '影视': 42, '苹果': 43, '帅哥': 44, '女生专用': 45, '宗教': 46,
            '节日': 47, '可爱': 48, '正能量': 49, '系统': 50, '文字': 51, '情侣': 52, '简约': 53, '麻匪': 54,
        }
        self.keys = self.classes.keys()
        self.headers = {
            "UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0",
        }

    def main(self, class_name):
        page = 0
        url = f"http://m.netbian.com/e/extend/mlist.php?page={page}&classid={self.classes.get(class_name)}&tempid=12&line=17"
        req = requests.get(url)
        while page <= 1:
            try:
                category = Category.objects.get(name=class_name)
            except:
                category = Category(name=class_name)
                category.save()
            try:
                tree = etree.HTML(req.text)
                imgs = tree.xpath("//li/a/img")
            except AttributeError as e:
                print(f'共{page}页，已完成...')
                break
            for i in imgs:
                img = i.attrib
                src = img.get("src")
                alt = img.get("alt")
                try:
                    wallpaper = Wallpaper(category=category, tag=alt.replace(" ", ","), url=src, title=alt)
                    wallpaper.save()
                    print(alt, src)
                except:
                    print("已存在")
                    return
            page += 1
            url = f"http://m.netbian.com/e/extend/mlist.php?page={page}&classid={self.classes.get(class_name, 2)}&tempid=12&line=17"
            req = requests.get(url)


if __name__ == "__main__":
    NetBiAnWallpaperSpider().main("动漫")
